<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>并发基石-Markword与锁升级 | Eetal&#39;s Blog</title>

  <!-- keywords -->
  

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="欢迎查看Eetal的第十一篇博客–并发基石-Markword与锁升级 synchronizedsynchronized关键字是java提供的互斥锁关键字，我们常说的互斥锁一般都是非自旋锁，即竞争不到锁的线程会进入阻塞状态知道被唤醒今天我们来讲讲java中用来对synchronized进行优化的三种锁，同时会介绍markword对象头目前我在网上搜到的十几篇博客讲的都有问题,可能有写对的我没搜到.很">
<meta property="og:type" content="article">
<meta property="og:title" content="并发基石-Markword与锁升级">
<meta property="og:url" content="http://eetal.gitee.io/blog/2019/03/24/并发基石-Markword与锁升级/index.html">
<meta property="og:site_name" content="Eetal&#39;s Blog">
<meta property="og:description" content="欢迎查看Eetal的第十一篇博客–并发基石-Markword与锁升级 synchronizedsynchronized关键字是java提供的互斥锁关键字，我们常说的互斥锁一般都是非自旋锁，即竞争不到锁的线程会进入阻塞状态知道被唤醒今天我们来讲讲java中用来对synchronized进行优化的三种锁，同时会介绍markword对象头目前我在网上搜到的十几篇博客讲的都有问题,可能有写对的我没搜到.很">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2019-03-26T03:29:41.673Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="并发基石-Markword与锁升级">
<meta name="twitter:description" content="欢迎查看Eetal的第十一篇博客–并发基石-Markword与锁升级 synchronizedsynchronized关键字是java提供的互斥锁关键字，我们常说的互斥锁一般都是非自旋锁，即竞争不到锁的线程会进入阻塞状态知道被唤醒今天我们来讲讲java中用来对synchronized进行优化的三种锁，同时会介绍markword对象头目前我在网上搜到的十几篇博客讲的都有问题,可能有写对的我没搜到.很">
  
    <link rel="alternative" href="/atom.xml" title="Eetal&#39;s Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="http://www.yangyitao.top/FiveChess/resource/ico/favicon.ico">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  
  

  <script src="//cdn.bootcss.com/require.js/2.3.2/require.min.js"></script>
  <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

  
</head>
<body>
  <div id="container">
    <div id="particles-js"></div>
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
	<header id="header" class="inner">
		<a href="/" class="profilepic">
			
			<img lazy-src="/blog/images/head.jpg" class="js-avatar">
			
		</a>

		<hgroup>
		  <h1 class="header-author"><a href="/">Eetal</a></h1>
		</hgroup>

		
		<p class="header-subtitle">java coder</p>
		

		
			<div class="switch-btn">
				<div class="icon">
					<div class="icon-ctn">
						<div class="icon-wrap icon-house" data-idx="0">
							<div class="birdhouse"></div>
							<div class="birdhouse_holes"></div>
						</div>
						<div class="icon-wrap icon-ribbon hide" data-idx="1">
							<div class="ribbon"></div>
						</div>
						
						<div class="icon-wrap icon-link hide" data-idx="2">
							<div class="loopback_l"></div>
							<div class="loopback_r"></div>
						</div>
						
						
					</div>
					
				</div>
				<div class="tips-box hide">
					<div class="tips-arrow"></div>
					<ul class="tips-inner">
						<li>菜单</li>
						<li>标签</li>
						
						<li>友情链接</li>
						
						
					</ul>
				</div>
			</div>
		

		<div class="switch-area">
			<div class="switch-wrap">
				<section class="switch-part switch-part1">
					<nav class="header-menu">
						<ul>
						
							<li><a href="/blog/">Home</a></li>
				        
							<li><a href="/blog/archives">Archives</a></li>
				        
						</ul>
					</nav>
					<nav class="header-nav">
						<div class="social">
							
						</div>
					</nav>
				</section>
				
				
				<section class="switch-part switch-part2">
					<div class="widget tagcloud" id="js-tagcloud">
						
					</div>
				</section>
				
				
				
				<section class="switch-part switch-part3">
					<div id="js-friends">
					
			          <a target="_blank" class="main-nav-link switch-friends-link" href="https://github.com/smackgg/hexo-theme-smackdown">smackdown</a>
			        
			        </div>
				</section>
				

				
			</div>
		</div>
	</header>				
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
  	<div class="overlay">
  		<div class="slider-trigger"></div>
  		<h1 class="header-author js-mobile-header hide">Eetal</h1>
  	</div>
	<div class="intrude-less">
		<header id="header" class="inner">
			<div class="profilepic">
				<img lazy-src="/blog/images/head.jpg" class="js-avatar">
			</div>
			<hgroup>
			  <h1 class="header-author">Eetal</h1>
			</hgroup>
			
			<p class="header-subtitle">java coder</p>
			
			<nav class="header-menu">
				<ul>
				
					<li><a href="/blog/">Home</a></li>
		        
					<li><a href="/blog/archives">Archives</a></li>
		        
		        <div class="clearfix"></div>
				</ul>
			</nav>
			<nav class="header-nav">
				<div class="social">
					
				</div>
			</nav>
		</header>				
	</div>
</nav>
      <div class="body-wrap"><article id="post-并发基石-Markword与锁升级" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/blog/2019/03/24/并发基石-Markword与锁升级/" class="article-date">
  	<time datetime="2019-03-24T04:17:57.599Z" itemprop="datePublished">2019-03-24</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      并发基石-Markword与锁升级
      
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
        

        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>欢迎查看Eetal的第十一篇博客–并发基石-Markword与锁升级</p>
<h2 id="synchronized"><a href="#synchronized" class="headerlink" title="synchronized"></a>synchronized</h2><p>synchronized关键字是java提供的互斥锁关键字，我们常说的互斥锁一般都是非自旋锁，即竞争不到锁的线程会进入阻塞状态知道被唤醒<br>今天我们来讲讲java中用来对synchronized进行优化的三种锁，同时会介绍markword对象头<br>目前我在网上搜到的十几篇博客讲的都有问题,可能有写对的我没搜到.<br>很多人不经过验证直接把markOop.hpp中的JavaThread*当成ThreadId这是错误的，实际是java线程在C语言的指针<br>并且未计算过hashCode和计算过hashCode的情况也是不一样的<br>本篇博客最后会展示使用jol工具，读取展示对象头的结果进行验证<br>附上openjdk的markOop.hpp<a href="http://hg.openjdk.java.net/jdk8/jdk8/hotspot/file/87ee5ee27509/src/share/vm/oops/markOop.hpp" target="_blank" rel="noopener">链接</a></p>
<h2 id="对象头Markword"><a href="#对象头Markword" class="headerlink" title="对象头Markword"></a>对象头Markword</h2><p>对象头是java将对象比较常用和重要的运行时数据，如hashCode、gc标识、存活年龄等等进行集中存储的一组数据<br>占据8个字节，以下只讨论目前比较常见的64位的情况</p>
<h2 id="偏向锁、轻量级锁、重量级锁介绍"><a href="#偏向锁、轻量级锁、重量级锁介绍" class="headerlink" title="偏向锁、轻量级锁、重量级锁介绍"></a>偏向锁、轻量级锁、重量级锁介绍</h2><p>64bit下各锁状态的Markword格式<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">64 bits:</span><br><span class="line">  --------</span><br><span class="line">  unused:25 <span class="built_in">hash</span>:31 --&gt;| unused:1   age:4    biased_lock:1 lock:2 (normal object)</span><br><span class="line">  JavaThread*:54 epoch:2 unused:1   age:4    biased_lock:1 lock:2 (biased object)</span><br><span class="line">  PromotedObject*:61 ---------------------&gt;| promo_bits:3 -----&gt;| (CMS promoted object)</span><br><span class="line">  size:64 -----------------------------------------------------&gt;| (CMS free block)</span><br><span class="line"> </span><br><span class="line">  unused:25 <span class="built_in">hash</span>:31 --&gt;| cms_free:1 age:4    biased_lock:1 lock:2 (COOPs &amp;&amp; normal object)</span><br><span class="line">  JavaThread*:54 epoch:2 cms_free:1 age:4    biased_lock:1 lock:2 (COOPs &amp;&amp; biased object)</span><br><span class="line">  narrowOop:32 unused:24 cms_free:1 unused:4 promo_bits:3 -----&gt;| (COOPs &amp;&amp; CMS promoted object)</span><br><span class="line">  unused:21 size:35 --&gt;| cms_free:1 unused:7 ------------------&gt;| (COOPs &amp;&amp; CMS free block)</span><br><span class="line"> [ptr             | 00]  locked             ptr points to real header on stack</span><br><span class="line"> [header      | 0 | 01]  unlocked           regular object header</span><br><span class="line"> [ptr             | 10]  monitor            inflated lock (header is wapped out)</span><br><span class="line"> [ptr             | 11]  marked             used by markSweep to mark an object</span><br></pre></td></tr></table></figure></p>
<p>32bit下各锁状态的Markword格式<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">32 bits:</span><br><span class="line">  --------</span><br><span class="line">  <span class="built_in">hash</span>:25 ------------&gt;| age:4    biased_lock:1 lock:2 (normal object)</span><br><span class="line">  JavaThread*:23 epoch:2 age:4    biased_lock:1 lock:2 (biased object)</span><br><span class="line">  size:32 ------------------------------------------&gt;| (CMS free block)</span><br><span class="line">  PromotedObject*:29 ----------&gt;| promo_bits:3 -----&gt;| (CMS promoted object)</span><br></pre></td></tr></table></figure></p>
<p>也就是对象头的最后两位，是作为锁的状态标志<br>00—轻量锁<br>01—偏向锁/无锁<br>10—重量锁<br>11—GC标记<br>偏向锁状态和无锁状态通过区分对象头倒数第三位来确定，0代表无锁，1代表偏向锁<br>注意，未计算hashCode的对象的初始状态为匿名偏向锁（线程指针为0，代表无线程获取）而非无锁</p>
<h3 id="偏向锁与无锁"><a href="#偏向锁与无锁" class="headerlink" title="偏向锁与无锁"></a>偏向锁与无锁</h3><p>java中对于锁的实际获取依赖于UnSafe调用native方法去实现不同操作系统上的cas原语操作<br>如果一个对象，在每次作业的运行始终处于单一线程，那每次对于锁的检测、获取和释放都会对性能造成不小的消耗<br>于是java引入了偏向锁<br>当一个处于匿名偏向锁状态的对象，第一次被一个线程竞争时,其对象头会被标记为偏向锁,同时存储其线程指针<br>接下来每次该线程对该锁的获取都不需要经过不必要的cas判断锁资源从而优化了性能，这也是偏向的由来</p>
<p>匿名偏向锁状态的对象如果计算了hashCode，则会变为无锁状态。hashCode存在markword，并且接下来不会再进去偏向锁</p>
<p>匿名偏向锁状态的对象被获取时，进入非匿名偏向锁状态，markword存储持有者的java线程在操作系统的C语言指针</p>
<p>无锁状态下的对象被获取时，会直接跳到轻量级锁（因为偏向锁下markword没有记录hashCode，没办法存储hashCode,而轻量级锁的下面讲）</p>
<p>非匿名偏向锁状态的对象计算了hashCode以后,会直接进入重量级锁，此时的重量级锁会（重量级锁的下面讲）</p>
<p>开启偏向锁的支持需要添加两个虚拟机参数<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">-XX:+UseBiasedLocking</span><br><span class="line">-XX:BiasedLockingStartupDelay=0</span><br></pre></td></tr></table></figure></p>
<p>第一个参数是开启偏向锁<br>第二个参数是指定立即开启，因为默认偏向锁的开启时在虚拟机运行后延时5秒<br>如果没有线程竞争，非匿名偏向锁偏向锁释放后会变回匿名偏向锁状态</p>
<h3 id="锁升级-轻量级锁"><a href="#锁升级-轻量级锁" class="headerlink" title="锁升级-轻量级锁"></a>锁升级-轻量级锁</h3><p>当一个对象处于非匿名偏向锁状态下，如果有别的线程过来竞争，另一个线程尝试竞争锁，竞争失败并给予jvm一个竞争的信号以后进入自旋（不断尝试获取锁）<br>接下来在持有该锁的线程执行来到安全点时，会触发stop the world并将膨胀为轻量级锁<br>轻量级锁会创建一份锁记录(Lock Record)在当前持有他的线程的线程栈里<br>LockRecord中包含一个owner属性指向锁对象，而锁对象的markword中也会保存一个执行该LockRecord的指针</p>
<p>这时的竞争就是轻量级锁的竞争了，轻量级锁的竞争时，竞争锁的线程会在一个周期时间内不断的自旋获取锁，如果获取失败就会进入阻塞并将markword的锁标记标记为10（重量级锁）</p>
<p>因为LockRecord复制了markword，所以在执行同步块时并不去关注markword，只有到了释放时</p>
<p>1.锁对象的markword升级为了重量级锁，将锁对象升级为重量级锁，锁对象的markword存储一个指向一个由操作系统实现的mutex互斥变量，唤醒阻塞的竞争线程</p>
<p>2.markword没变化，释放锁，对象锁恢复到无锁状态（如果LockRecord记录的是偏向锁，则恢复到匿名偏向锁，否则恢复到无锁状态）</p>
<h2 id="锁升级-重量级锁"><a href="#锁升级-重量级锁" class="headerlink" title="锁升级-重量级锁"></a>锁升级-重量级锁</h2><p>当对象来到重量级锁以后，新被从竞争队列挑选出来一部分竞争锁的线程队列会一起竞争锁<br>最终竞争到锁的一个线程会继续运行，竞争失败的线程进入阻塞队列<br>处于执行状态的线程执行完同步代码块后，会释放锁并唤醒阻塞队列中的线程<br>将他们加入新的挑选出来的竞争锁的线程队列，并重新竞争锁，重复以上操作<br>因为需要阻塞和唤醒线程，所以需要从用户态到系统态切换，所以重量级锁下的系统开销很大</p>
<h2 id="代码验证"><a href="#代码验证" class="headerlink" title="代码验证"></a>代码验证</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">Thread currentThread = Thread.currentThread();</span><br><span class="line">System.out.println(<span class="string">"threadId : "</span>+Long.toBinaryString(currentThread.getId()));</span><br><span class="line">Object o = new Object();</span><br><span class="line">System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">System.out.println(<span class="string">"init object info"</span>);</span><br><span class="line">System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">synchronized (o) &#123;</span><br><span class="line">	System.out.println(<span class="string">"synchronized lock object info"</span>);</span><br><span class="line">	System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">	System.out.println(<span class="string">"synchronized finished"</span>);</span><br><span class="line">	System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">&#125;</span><br><span class="line">Thread.sleep(2000);</span><br><span class="line">System.out.println(<span class="string">"after synchronized object info"</span>);</span><br><span class="line">System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">System.out.println(<span class="string">"binary hashCode : "</span>+Integer.toBinaryString(o.hashCode()));</span><br><span class="line">System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">"after calculate hashcode object info"</span>);</span><br><span class="line">System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line"></span><br><span class="line">synchronized (o) &#123;</span><br><span class="line">	System.out.println(<span class="string">"synchronized lock object info"</span>);</span><br><span class="line">	System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">	System.out.println(<span class="string">"synchronized finished"</span>);</span><br><span class="line">	System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">&#125;</span><br><span class="line">Object o2 = new Object();</span><br><span class="line">System.out.println(<span class="string">"o2 hashCode : "</span>+Long.toBinaryString(o2.hashCode()));</span><br><span class="line">System.out.println(<span class="string">"init lock object2 info"</span>);</span><br><span class="line">System.out.println(ClassLayout.parseInstance(o2).toPrintable());</span><br><span class="line">System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">synchronized (o2) &#123;</span><br><span class="line">	System.out.println(<span class="string">"synchronized lock object2 info"</span>);</span><br><span class="line">	System.out.println(ClassLayout.parseInstance(o2).toPrintable());</span><br><span class="line">	System.out.println(<span class="string">"synchronized finished"</span>);</span><br><span class="line">	System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">&#125;</span><br><span class="line">System.out.println(<span class="string">"after lock object2 info"</span>);</span><br><span class="line">System.out.println(ClassLayout.parseInstance(o2).toPrintable());</span><br><span class="line">System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">//计算过hashcode的会直接进入轻量锁</span><br></pre></td></tr></table></figure>
<p>输出结果<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line">threadId : 1</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">init object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">synchronized lock object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           05 e8 09 01 (00000101 11101000 00001001 00000001) (17426437)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">synchronized finished</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">after synchronized object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           05 e8 09 01 (00000101 11101000 00001001 00000001) (17426437)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">binary hashCode : 100000111110100010001111000001</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">after calculate hashcode object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           01 c1 23 fa (00000001 11000001 00100011 11111010) (-98320127)</span><br><span class="line">      4     4        (object header)                           20 00 00 00 (00100000 00000000 00000000 00000000) (32)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">synchronized lock object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           50 f7 c4 02 (01010000 11110111 11000100 00000010) (46462800)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">synchronized finished</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">o2 hashCode : 110101100000011100010111110011</span><br><span class="line">init lock object2 info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           01 f3 c5 81 (00000001 11110011 11000101 10000001) (-2117733631)</span><br><span class="line">      4     4        (object header)                           35 00 00 00 (00110101 00000000 00000000 00000000) (53)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">synchronized lock object2 info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           50 f7 c4 02 (01010000 11110111 11000100 00000010) (46462800)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">synchronized finished</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">after lock object2 info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           01 f3 c5 81 (00000001 11110011 11000101 10000001) (-2117733631)</span><br><span class="line">      4     4        (object header)                           35 00 00 00 (00110101 00000000 00000000 00000000) (53)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br></pre></td></tr></table></figure></p>
<p>多线程竞争<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">Thread currentThread = Thread.currentThread();</span><br><span class="line">		System.out.println(<span class="string">"threadId : "</span>+Long.toBinaryString(currentThread.getId()));</span><br><span class="line">		Object o = new Object();</span><br><span class="line">		System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">		System.out.println(<span class="string">"init object info"</span>);</span><br><span class="line">		System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">		System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">		</span><br><span class="line">		new <span class="function"><span class="title">Thread</span></span>() &#123;</span><br><span class="line">			public void <span class="function"><span class="title">run</span></span>() &#123;</span><br><span class="line">				synchronized (o) &#123;</span><br><span class="line">					System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">					System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">					System.out.println(<span class="string">"another thread id "</span>+Long.toBinaryString(this.getId()));</span><br><span class="line">					System.out.println(<span class="string">"synchronized lock object info"</span>);</span><br><span class="line">					System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">					System.out.println(<span class="string">"synchronized finished"</span>);</span><br><span class="line">					System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">					System.out.println(<span class="string">"------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;.start();</span><br><span class="line">		</span><br><span class="line">		synchronized (o) &#123;</span><br><span class="line">			System.out.println(<span class="string">"synchronized lock object info"</span>);</span><br><span class="line">			System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">			</span><br><span class="line">			//有锁未计算hashCode状态下计算hashCode</span><br><span class="line">			</span><br><span class="line">			o.hashCode();</span><br><span class="line">			System.out.println(<span class="string">"when synchronized calculate hashcode "</span>);</span><br><span class="line">			System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">			</span><br><span class="line">			System.out.println(<span class="string">"synchronized finished"</span>);</span><br><span class="line">			System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		Thread.sleep(2000);</span><br><span class="line">		System.out.println(<span class="string">"after synchronized object info"</span>);</span><br><span class="line">		System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">		System.out.println(<span class="string">"binary hashCode : "</span>+Integer.toBinaryString(o.hashCode()));</span><br><span class="line">		System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">		</span><br><span class="line">		System.out.println(<span class="string">"after calculate hashcode object info"</span>);</span><br><span class="line">		System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">		System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">		</span><br><span class="line">		synchronized (o) &#123;</span><br><span class="line">			System.out.println(<span class="string">"synchronized lock object info"</span>);</span><br><span class="line">			System.out.println(ClassLayout.parseInstance(o).toPrintable());</span><br><span class="line">			System.out.println(<span class="string">"synchronized finished"</span>);</span><br><span class="line">			System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		Object o2 = new Object();</span><br><span class="line">		System.out.println(<span class="string">"o2 hashCode : "</span>+Long.toBinaryString(o2.hashCode()));</span><br><span class="line">		System.out.println(<span class="string">"init lock object2 info"</span>);</span><br><span class="line">		System.out.println(ClassLayout.parseInstance(o2).toPrintable());</span><br><span class="line">		System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">		synchronized (o2) &#123;</span><br><span class="line">			System.out.println(<span class="string">"synchronized lock object2 info"</span>);</span><br><span class="line">			System.out.println(ClassLayout.parseInstance(o2).toPrintable());</span><br><span class="line">			System.out.println(<span class="string">"synchronized finished"</span>);</span><br><span class="line">			System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		System.out.println(<span class="string">"after lock object2 info"</span>);</span><br><span class="line">		System.out.println(ClassLayout.parseInstance(o2).toPrintable());</span><br><span class="line">		System.out.println(<span class="string">"-------------------------------------------------------------------------------------------------------"</span>);</span><br><span class="line">		//计算过hashcode的会直接进入轻量锁</span><br></pre></td></tr></table></figure></p>
<p>输出结果<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br></pre></td><td class="code"><pre><span class="line">threadId : 1</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">init object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">synchronized lock object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           ba c4 c4 02 (10111010 11000100 11000100 00000010) (46449850)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">when synchronized calculate hashcode </span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           ba c4 c4 02 (10111010 11000100 11000100 00000010) (46449850)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">synchronized finished</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">another thread id 1010</span><br><span class="line">synchronized lock object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           ba c4 c4 02 (10111010 11000100 11000100 00000010) (46449850)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">synchronized finished</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">------------------------------------------------------------------------------------------------------</span><br><span class="line">after synchronized object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           01 c1 23 fa (00000001 11000001 00100011 11111010) (-98320127)</span><br><span class="line">      4     4        (object header)                           20 00 00 00 (00100000 00000000 00000000 00000000) (32)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">binary hashCode : 100000111110100010001111000001</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">after calculate hashcode object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           01 c1 23 fa (00000001 11000001 00100011 11111010) (-98320127)</span><br><span class="line">      4     4        (object header)                           20 00 00 00 (00100000 00000000 00000000 00000000) (32)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">synchronized lock object info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           90 f5 ad 02 (10010000 11110101 10101101 00000010) (44955024)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">synchronized finished</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">o2 hashCode : 110101100000011100010111110011</span><br><span class="line">init lock object2 info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           01 f3 c5 81 (00000001 11110011 11000101 10000001) (-2117733631)</span><br><span class="line">      4     4        (object header)                           35 00 00 00 (00110101 00000000 00000000 00000000) (53)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">synchronized lock object2 info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           90 f5 ad 02 (10010000 11110101 10101101 00000010) (44955024)</span><br><span class="line">      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">synchronized finished</span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br><span class="line">after lock object2 info</span><br><span class="line">java.lang.Object object internals:</span><br><span class="line"> OFFSET  SIZE   TYPE DESCRIPTION                               VALUE</span><br><span class="line">      0     4        (object header)                           01 f3 c5 81 (00000001 11110011 11000101 10000001) (-2117733631)</span><br><span class="line">      4     4        (object header)                           35 00 00 00 (00110101 00000000 00000000 00000000) (53)</span><br><span class="line">      8     4        (object header)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)</span><br><span class="line">     12     4        (loss due to the next object alignment)</span><br><span class="line">Instance size: 16 bytes</span><br><span class="line">Space losses: 0 bytes internal + 4 bytes external = 4 bytes total</span><br><span class="line"></span><br><span class="line">-------------------------------------------------------------------------------------------------------</span><br></pre></td></tr></table></figure></p>
<h3 id="请移步"><a href="#请移步" class="headerlink" title="请移步"></a>请移步</h3><p>个人主页: <a href="http://yangyitao.top" target="_blank" rel="noopener">yangyitao.top</a></p>

      
    </div>
    
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog/2019/03/26/并发基石-AQS与Condition/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">&lt;</strong>
      <div class="article-nav-title">
        
          并发基石-AQS与Condition
        
      </div>
    </a>
  
  
    <a href="/blog/2019/03/23/HashMap、TreeMap、HashTable、ConcurrentHashMap对比/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">HashMap、TreeMap、HashTable、ConcurrentHashMap对比(jdk1.8)</div>
      <strong class="article-nav-caption">&gt;</strong>
    </a>
  
</nav>

  
</article>


<div class="ds-share share" data-thread-key="并发基石-Markword与锁升级" data-title="并发基石-Markword与锁升级" data-url="http://eetal.gitee.io/blog/blog/2019/03/24/并发基石-Markword与锁升级/"  data-images="/blog/images/head.jpg" data-content="并发基石-Markword与锁升级">
    <div class="ds-share-inline">
      <ul  class="ds-share-icons-16">
      	<li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
        <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
        <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
        <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
        <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>
      </ul>
      <div class="ds-share-icons-more">
      </div>
    </div>
 </div>
 





</div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info">
      <div class="footer-left">
        &copy; 2019 Eetal
      </div>
        <div class="footer-right">
          <a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/smackgg/hexo-theme-smackdown" target="_blank">Smackdown</a>
        </div>
    </div>
  </div>
</footer>
    </div>
    
  <link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.css">


<script>
	var yiliaConfig = {
		fancybox: true,
		mathjax: true,
		animate: true,
		isHome: false,
		isPost: true,
		isArchive: false,
		isTag: false,
		isCategory: false,
		open_in_new: false
	}
</script>
<script src="/blog/js/main.js"></script>



<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>


  </div>
</body>
</html>